<?php
$orderServices = new OrdersService();
$shippmentPlanService = new ShippmentPlanService();
$invoiceService = new InvoiceService();	
if(isset($_GET['opt']))
{
	$opt = $_GET['opt'];
	$moduleID = $moduleService->getModuleIDByFuncAndOpt($context, $result, $func, $opt);
	$permit = $permitService->checkPermition($context, $result, $_SESSION['user']['type'], $moduleID);
	$shipmentService = new ShipmentService();
	if($opt == "plan" && $permit ==true)
	{
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countByStatus($context, $result, "4");
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllOrderByTypeByRange($context, $result, "4", $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/shipPlan.tpl");
	}
	
	if($opt == "history" && $permit ==true)
	{
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countByStatus($context, $result, "4");
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllOrderByTypeByRange($context, $result, "4", $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listShipPlanHistory.tpl");
	}

	if($opt == "shipPlanHistory")
	{
		$fullDateArr = array();		
		$dateArr = $shipmentService->getListShipmentDate($context, $result, $_GET['id']);
		foreach($dateArr as $date)
		{		
			$fullDateArr[$date] = $invoiceService->getListInvoiceByDate($context, $result, $_GET['id'], $date);
		}
		$smarty->assign('orderNumber', $_GET['orderNumber']);
		$smarty->assign('fullDateArr', $fullDateArr);
		$smarty->display($templateDir . "/temp1/listShipPlanHistoryDetail.tpl");
	}
	
	if($opt == "planF" && $permit ==true)
	{
		$factID = $_SESSION['user']['ID'];
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllShippmentPlanByFactID($context, $result, $factID);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllShippmentPlanByRangeAndFactID($context, $result, $start, $configPage['limit'], $factID);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/shipPlan.tpl");
	}

	if($opt == "addPlan" && $permit ==true)
	{
		if(isset($_GET['id']))
		{
			$order = $orderServices->getOrderByOid($context, $result, $_GET['id']);
			//need edit type to 7,8,9
			$type = $order->getOrdersDomain()->getType();
			if($type == 7 || $type == 8 || $type == 9)
			{
				$orderDetailArr = $order->getOrderDetail();
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('order', $order);
			}

			$smarty->display($templateDir . "/temp1/addShipPlan.tpl");
		}
	}
		
	if($opt == "shipPlanOrder" && $permit ==true)
	{
		if(isset($_GET['id']))
		{
			$orderID = $_GET['id'];
			$shippedArr = array();
			$order = $orderServices->getOrderByOrderID($context, $result, $orderID);
			foreach ($order->getOrderDetail() as $orderDetail)
			{
				$shipped = $shipmentService->getShippedQuantity($context, $result, $orderID, $orderDetail->getArticle()->getOid());
				$shippedArr[$orderDetail->getArticle()->getOid()] = $shipped;
			}
			
			$smarty->assign("shippedArr", $shippedArr);
			$smarty->assign("order", $order);
			$smarty->display($templateDir . "/temp1/listShipPlanOfOrder.tpl");
		}
	}
		
	if($opt == "viewShipPlan" && $permit ==true)
	{
		if(isset($_GET['sid']) && isset($_GET['oid']))
		{
			$shipID = $_GET['sid'];
			$orderID = $_GET['oid'];
			$shippment = $shippmentPlanService->getShippmentByShipIDAndOrderID($context, $result, $shipID, $orderID);
			$order = $shippment->getOrderArr();
			$type = $order[0]->getOrdersDomain()->getType();
			if($type == 7 || $type == 8 || $type == 9)
			{
				$orderDetailArr = $order[0]->getOrderDetail();
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign("shippment", $shippment);
			}
			$smarty->display($templateDir . "/temp1/viewShipPlanOfOrder.tpl");
		}
	}
}
?>